package cn.rxls.community.util;


import cn.rxls.community.enums.ResultEnum;
import cn.rxls.community.whitelist.FileProperties;
import cn.rxls.community.exception.BusinessException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 黑名单过滤
 */
@Component
public class WhitelistFiltering {

    @Resource
    FileProperties fileProperties;


    //文件后准过滤
    public void file(String fileName) {
        //获取白名单
        List<String> blacklistedExtensions = fileProperties.getBlacklistedExtensions();

        //开始检查
        List<String> collect = blacklistedExtensions.stream().filter(fileName::contains).collect(Collectors.toList());

        //是否符合白名单规则
        if (collect.size() == 0) {
            throw new BusinessException(ResultEnum.ERROR_UPLOAD_IMG_TYPE);
        }
    }

}
